<?php
/**
 * This file is part of the symfony package.
 * Copyright (c) 2007 Dino Co.,Ltd.
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Yoshihiro TAKAHARA <takahara@dino.co.jp
 * @version    SVN: $Id$
 *
 *
 *
 *
 *
 *
 */
class sfPageFlowActions extends sfActions
{

    public function acceptRequests($vars){
        $this->flow->acceptRequests($vars);
    }
    public function executeFlow()
    {
        $this->flow->doEvent();
        $this->flowId = $this->flow->flowId;

        return $this->flow->execute();
    }

    public function executeDisplay()
    {
        $this->flowId = $this->flow->flowId;

        return $this->flow->display();
    }

    // handle validate Error
    public function handleError(){
        $this->flow->transitOnError();
        return $this->flow->execute();
    }
    public function initialize($context){
        $module = sfContext::getInstance()->getModuleName();

        if(parent::initialize($context)){
            $this->flow = sfPageFlow::getInstance($this
                                                  ,sprintf("%s/flow",$module), self::getFlowId());

            return true;
        }
        return false;
    }

    protected static function getFlowId() {
        $flowId = sfContext::getInstance()->getRequest()->getParameter("flow_id",null);
        if (!isset($flowId)) {
            $flowId = sfContext::getInstance()->getRequest()->getAttribute("flow_id",null);
            if (!isset($flowId)) {
                $flowId = self::getRandomString();
            }
        }

        return $flowId;
    }

    private static function getRandomString($nLengthRequired = 8){
        $sCharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
        mt_srand();

        $sRes = "";
        for ($i=0; $i<$nLengthRequired; $i++) {
            $sRes .= $sCharList{mt_rand(0, strlen($sCharList) - 1)};
        }

        return $sRes;
    }

}

